import { _decorator, Component, Node, Size, view, AudioClip, Animation } from 'cc';
import { AudioMgr } from './AudioMgr';
const { ccclass, property } = _decorator;

@ccclass('enemy1')
export class enemy1 extends Component {
    
    // 下降速度
    speed: number = 10;

    // 生命值
    hp: number = 1;

    // 死亡动画
    dieAnimation: string = 'enemy1_die'

    // 可视区域
    viewSize: Size;
    
    @property(AudioClip)
    dieAudio: AudioClip;
    
    start() {
        this.viewSize = view.getVisibleSize()
    }


    update(deltaTime: number) {
        this.run(deltaTime)
    }


    // run
    run(dt: number) {
        let p = this.node.getPosition()

        p.y -= this.speed * 50 * dt;

        if (p.y < - this.viewSize.height - 200) {
            this.node.destroy()
            return
        }

        this.node.setPosition(p)
    }


    die() {
        // 播放死亡声音和动画
        AudioMgr.inst.playOneShot(this.dieAudio)

        // 死亡动画
        this.node.getComponent(Animation).play(this.dieAnimation)

        // 销毁节点, 等待播放完成后销毁
        this.scheduleOnce(() => this.node.destroy(), 1.2)

    }


}


